package sindconsole.misc.webapp.controller;

import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import sindconsole.misc.service.OperationService;

import java.util.Date;

@Slf4j
@RestController
@RequestMapping("/api/misc/op")
@RequiresPermissions("op-log:r")
public class OperationController {

    @Autowired
    OperationService operationService;

    @GetMapping("/traces")
    public Object listTraces(@RequestParam(value = "operation", required = false) String operation,
                             @RequestParam(value = "userId", required = false) Long userId,
                             @RequestParam(value = "loggedTime0", required = false) @DateTimeFormat(pattern="yyyy-MM-dd HH:mm:ss") Date loggedTime0,
                             @RequestParam(value = "loggedTime1", required = false) @DateTimeFormat(pattern="yyyy-MM-dd HH:mm:ss") Date loggedTime1,
                             @RequestParam(value = "loggedAddr", required = false) String loggedAddr,
                             @RequestParam(value = "page", defaultValue = "0") int page,
                             @RequestParam(value = "pageSize", required = false) Integer pageSize) {
        int[] pagination = pageSize != null ? new int[] {page, pageSize} : new int[] {page};
        return operationService.listTraces(operation, userId, loggedTime0, loggedTime1, loggedAddr, pagination);
    }

}
